package com.bw.inspection.page

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.common.event.CustomEvent
import com.bw.common.view.BaseMvvmActivity
import com.bw.inspection.R
import com.bw.inspection.adapter.InspectionAdapter
import com.bw.inspection.databinding.ActivityInspectionBinding
import com.bw.inspection.vm.InspectionViewModel
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 点位管理页面
 */
@Route(path = RouterPath.PATH_INSPECTION_PATH)
@AndroidEntryPoint
class InspectionActivity : BaseMvvmActivity<ActivityInspectionBinding>() {
    val mAdapter = InspectionAdapter()
    private val mViewModel: InspectionViewModel by viewModels()

    override fun getLayoutId(): Int = R.layout.activity_inspection
    override fun initViews() {
        super.initViews()
        mDataBinding.inRv.adapter = mAdapter
        mDataBinding.inRv.layoutManager = LinearLayoutManager(this)

        mDataBinding.back.setOnClickListener { finish() }
        mDataBinding.fab.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_ADD_INSPECTION_PATH).navigation()
        }
        //跳转到详情页
        mAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(RouterPath.PATH_INSPECTION_DETAIL_PATH)
                .withInt("xid",mAdapter.data[position].id)
                .navigation()
        }

        //跳转到巡检代办
        mDataBinding.addDevice.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_INSPECTION_NO_PATH).navigation()
        }


    }

    override fun loadData() {
        super.loadData()

        mViewModel.getInspectionData()
        mViewModel.mInspectionSuccess.observe(this){
            mAdapter.data.clear()
            mAdapter.data.addAll(it.reversed())
            mAdapter.notifyDataSetChanged()
        }
    }




    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onCusEventBus(event: CustomEvent){
        if (event.target == RouterPath.PATH_INSPECTION_PATH){
            if (event.type==1){
                mViewModel.getInspectionData()
            }
        }
    }
}